package com.ofhb.webapp.client.ui.grid;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.ofhb.webapp.client.ui.grid.events.PageEmptyEvent;
import com.ofhb.webapp.client.ui.grid.events.RowDeleteEvent;
import com.ofhb.webapp.client.utils.EventUtil;

public class GridDataTable extends FlexTable {
	
	private int selectedRow = -1;
	private GridData gridData = null;
	
	public GridDataTable() {
		super();
		sinkEvents(Event.ONMOUSEOVER | Event.ONMOUSEOUT);
		
		addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				Cell cell = getCellForEvent(event);
				if(cell != null) {
					selectedRow = cell.getRowIndex();
					
					int rowCount = getRowCount();
					for (int i = 0; i < rowCount; i++) {
						//Document.get().getElementById(String.valueOf(i)).setClassName("app-ft-data-row");
						getRowFormatter().setStyleName(i, "app-ft-data-row");
					}
					
					getRowFormatter().setStyleName(selectedRow, "app-ft-data-row-sel");
	//				AppUtils.EVENT_BUS.fireEvent(new RowSelectEvent(selectedRow));
				}
			}
		});
	}

	@Override
	public void onBrowserEvent(Event event) {
		super.onBrowserEvent(event);
		Element td = getEventTargetCell(event);
		if (td == null) {
			return;
		}
		Element tr = DOM.getParent(td);
		switch (DOM.eventGetType(event)) {
			case Event.ONMOUSEOVER: {
				if (selectedRow >= 0 && tr.getId().equalsIgnoreCase(getRowFormatter().getElement(selectedRow).getId())) {
					tr.setClassName("app-ft-data-row-sel");
					
				} else {
					tr.setClassName("app-ft-data-row-hover");
				}
				break;
			}
			case Event.ONMOUSEOUT: {
				if (selectedRow >= 0 && tr.getId().equalsIgnoreCase(getRowFormatter().getElement(selectedRow).getId())) {
					tr.setClassName("app-ft-data-row-sel");
				} else {
					tr.setClassName("app-ft-data-row");
				}
				break;
			}
		}
	}

	public void setData(GridData gridData) {
		this.gridData = gridData;
		render();
	}
	
	protected void render() {
		List<GridHeaderCell> headerRow = gridData.getGridHeaderRow();
		removeAllRows();
		List<ArrayList<String>> rows = gridData.getGridRows();
		int row = 0;
		int col = 0;
		String rowId = null;
		boolean colReset = true;
		if(rows.size() > 0 ) {
			for (ArrayList<String> arrayList : rows) {
				for (String string : arrayList) {
					
					if(colReset) {
						rowId = string;
						colReset = false;
					} else {
						Label lblDataCell = new Label(string);
						lblDataCell.setWidth(headerRow.get(col).getLength()+"px");
						lblDataCell.getElement().getStyle().setMargin(2, Unit.PX);
						setWidget(row, col, lblDataCell);
						getCellFormatter().setStyleName(row, col, "app-ft-data-cell");
					}
					col++;
	            }
				setWidget(row, col, getDeleteButtonForRowId(rowId, row));
				getCellFormatter().setStyleName(row, col, "app-ft-data-cell");
				
				col = 0;
				colReset = true;
				getRowFormatter().setStyleName(row, "app-ft-data-row");
				getRowFormatter().getElement(row).setId(rowId);
				row++;
			}
		} else {
			PageEmptyEvent pageEmptyEvent = new PageEmptyEvent();
			EventUtil.EVENT_BUS.fireEvent(pageEmptyEvent);
		}
	}
	
	private Button getDeleteButtonForRowId(final String recordId, final int rowNumber) {
		Button btnDelete = new Button("X");
		btnDelete.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				selectedRow = -1;
				RowDeleteEvent rowDeleteEvent = new RowDeleteEvent(rowNumber);
				EventUtil.EVENT_BUS.fireEvent(rowDeleteEvent);
			}
		});
		return btnDelete;
	}
}